﻿using Mengsk.ProxyDelivery.Service;
using Mengsk.ProxyDelivery.Service.Spider;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Goods
{
    /// <summary>
    /// GoodsFixUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class GoodsFixUserControl : UserControl, IPlugin
    {
        private ShoesService ss = ServiceContainer.GetService<ShoesService>();
        private System.Collections.ObjectModel.ObservableCollection<GoodFixViewModel> goods = new System.Collections.ObjectModel.ObservableCollection<GoodFixViewModel>();

        public GoodsFixUserControl()
        {
            InitializeComponent();
        }

        public string Title
        {
            get { return "商品图片修正"; }
        }

        public Control UIControl
        {
            get { return this; }
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int page = 0;
                var dt = this.dpEnd.Value == null ? DateTime.MinValue : this.dpEnd.Value.Value;

                while (true)
                {
                    var ret = ss.GetWrongShoes(dt, page, 30);
                    if (ret.Datas.Count < 1)
                    {
                        break;
                    }
                    foreach (var og in ret.Datas)
                    {
                        this.goods.Add(new GoodFixViewModel { Source = og, State = "" });
                    }
                    this.dgvGoods.ScrollIntoView(this.goods.Last());
                    App.DoEvents();
                }
                MessageBox.Show("下载完成");
            }
            catch (Exception ex)
            {
                if (ex.Message.Equals("NO_MORE_DATA") == false)
                {
                    MessageBox.Show(ex.Message);
                }
                else
                {
                    MessageBox.Show("下载完成");
                }
            }
        }

        private void btnProcess_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                List<Mengsk.ProxyDelivery.Domain.Vendor> vendors = new List<ProxyDelivery.Domain.Vendor>();
                var vs = ServiceContainer.GetService<VendorService>();

                foreach (var og in this.goods)
                {
                    og.State = "处理中...";
                    App.DoEvents();
                    var vendor = vendors.FirstOrDefault(obj => obj.Id == og.Source.VendorId);
                    if (vendor == null)
                    {
                        vendor = vs.GetById(og.Source.VendorId);
                        if (vendor == null)
                        {
                            og.State = "厂家不存在";
                            continue;
                        }
                        vendors.Add(vendor);
                    }

                    SpiderBase sb = SpiderBase.CreateSpider(vendor.Spider);
                    var shoess = sb.GetVendorShoes(vendor, og.Source.Number);
                    var s = shoess.FirstOrDefault(obj => obj.Type == og.Source.Type && obj.Number == og.Source.Number);
                    if (s == null)
                    {
                        og.State = "Go2上未找到相应的商品";
                        continue;
                    }
                    og.Source.Image = s.Image;
                    og.Source.Price = s.Price;
                    ServiceContainer.GetService<ShoesService>().Update(og.Source);
                    og.State = "处理成功";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
